package com.experience.util.rsa;

public class RSAUtil {

	public static void main(String[] args) throws Exception {
		String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCkeUsneR/CbnO02Po/h/wZy8J8obIQgbqkJnYxt0gbvR/atw6xUL4px2qOdcDwyIXf8QGWdBIRv+WhhqyFxKnd9Sbd9vh99Sg3gDAH0MFknEGtxHbBQmfzgeryiiRnw3DfskTDApLNOFE36Bjw+GTHvRrYW6fgXsLdZBhDdcBkAwIDAQAB";
		String privateKey = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAKR5Syd5H8Juc7TY+j+H/BnLwnyhshCBuqQmdjG3SBu9H9q3DrFQvinHao51wPDIhd/xAZZ0EhG/5aGGrIXEqd31Jt32+H31KDeAMAfQwWScQa3EdsFCZ/OB6vKKJGfDcN+yRMMCks04UTfoGPD4ZMe9Gthbp+Bewt1kGEN1wGQDAgMBAAECgYB4i5Gih6rYVR2mwZSANcl2mpmbwOMCKCLEJpBgFcXa1r1fDuXM3bHSax7U9McJct5z6G7Sd5W6vputzEfr7tzT7QzdaaqkXKvio75EyXgDuVILno45sGhN6I/StZSw5lreLnzsTv6f07dOnVbtKaRtU+xhoOyfOgGXKbqRt+vpcQJBAM3qR0cE5CsRnRql7IcXnFQ+K51yclA/3I92lrEMQcD29+IfYBR/O8EHbXBqlgiH7Kan8m/8C++0BI/STMVF9BkCQQDMepYQ9OKR181NYsrI1IZ4ANasJGLoUWzEIWaqRAZrpHiBoPjOWWocIP2R/EW2EIxmCc9ilz2RoJCH0La/KHx7AkAyzKFY+/PuTCId+OEpUsHWZqAL77gT5C1dlzwF4ag3GrZ8qpDhfaIYNOTi3ui0ohPX6Yb96YhoUljtwkv+PwvBAkBbd6qRQznmi2Ym675SP9825wkgT9Z+nY3N3jMSjwNgO148fklN5Ol2U7uaBgMZAiYLru+55nWAIxcD6fUVZYLpAkEAmd79E+kuz6OY3j/9MiYgdFophRQ2z8y2hsc3JgPx89Jznd/Xc7HNynI/komm7yBfUL3xdpPNkx4AStoZ3WWZ/g==";

		//签名原字符串
		String plainText = "123456";
		
		//签名
		String signstr = privateKeySign(privateKey,plainText);
		System.out.println(signstr);
		
		//验证签名
		System.out.println(doCheck(publicKey,plainText,signstr));
	}

	// 公钥加密过程
	public static String publicKeyEncrypt(String publicKey, String plainText) throws Exception {
		byte[] cipherData = RSAEncrypt.encrypt(RSAEncrypt.loadPublicKeyByStr(publicKey), plainText.getBytes());
		String cipher = Base64.encode(cipherData);
		return cipher;
	}

	// 私钥解密过程
	public static String privateKeyDecrypt(String privateKey, String cipher) throws Exception {
		byte[] res = RSAEncrypt.decrypt(RSAEncrypt.loadPrivateKeyByStr(privateKey), Base64.decode(cipher));
		String restr = new String(res);
		return restr;
	}

	// 私钥加密过程
	public static String privateKeyEncrypt(String privateKey, String plainText) throws Exception {
		byte[] cipherData = RSAEncrypt.encrypt(RSAEncrypt.loadPrivateKeyByStr(privateKey), plainText.getBytes());
		String cipher = Base64.encode(cipherData);
		return cipher;
	}

	// 公钥解密过程
	public static String publicKeyDecrypt(String publicKey, String cipher) throws Exception {
		byte[] res = RSAEncrypt.decrypt(RSAEncrypt.loadPublicKeyByStr(publicKey), Base64.decode(cipher));
		String restr = Base64.encode(res);
		return restr;
	}

	// 私钥签名过程
	public static String privateKeySign(String privateKey, String content) throws Exception {
		String signstr = RSASignature.sign(content, privateKey);
		return signstr;
	}

	// 验签
	public static Boolean doCheck(String publicKey, String content, String signstr) throws Exception {
		Boolean bool = RSASignature.doCheck(content, signstr, publicKey);
		return bool;
	}
}
